<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Anwsion dev team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/


if (!defined('IN_ANWSION'))
{
	die;
}

class edit extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action['actions'] = array();
		return $rule_action;
	}

	public function setup()
	{
		$this->crumb('信息发布', '/publish/');
	}

	public function index_action()
	{
		if ($_GET['special_id'])
		{
			if (!$special_info = $this->model('special')->get_special_info_by_id(intval($_GET['special_id']),false))
			{
				H::redirect_msg("指定专辑不存在", '/special/' . $_GET['special_id']);
			}
			if (!$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'] AND !$this->user_info['permission']['edit_special'])
			{
				if ($special_info['published_uid'] != $this->user_id)
				{
					H::redirect_msg("你没有权限编辑这个专辑", '/special/' . $_GET['special_id']);
				}
			}

			TPL::assign('special_info', $special_info);
		}
		else if (!$this->user_info['permission']['publish_special'] AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
		{
			H::redirect_msg('你所在用户组没有权限发布专辑');
		}
		else if ($this->is_post())
		{
			TPL::assign('special_info', array(
				'special_content' => $_POST['special_content'],
				'special_detail' => $_POST['special_detail']
			));
			
			$special_info['category_id'] = $_POST['category_id'];
		}
		
		if ($this->user_info['integral'] <= 0 AND get_setting('integral_system_enabled') == 'Y' AND !$_GET['special_id'])
		{
			H::redirect_msg('你的剩余积分已经不足以进行此操作');
		}
		
		TPL::assign('attach_access_key', md5($this->user_id . time()));
		
		if (!$special_info['category_id'] AND $_GET['category_id'])
		{
			$special_info['category_id'] = $_GET['category_id'];
		}
		
		if (get_setting('category_enable') == 'Y')
		{
			TPL::assign('special_category_list', $this->model('system')->build_category_html('special', 0, $special_info['category_id']));
		}
		
// 		if ($modify_reason = $this->model('special')->get_modify_reason())
// 		{
// 			TPL::assign('modify_reason', $modify_reason);
// 		}
		
		TPL::assign('human_valid', human_valid('special_valid_hour'));
		
		TPL::import_css('css/main.css');
		TPL::import_js('js/publish.js');
		TPL::import_js('js/ajaxupload.js');
		
		if (get_setting('advanced_editor_enable') == 'Y')
		{
			TPL::import_js('js/markItUp.js');
		}
		
		TPL::assign('user_open_info', $this->user_open_info);
// 		print_r($this->special_info);
// 		exit;
		TPL::output('special/edit_special');
	}
	
	public function wait_approval_action()
	{
		if ($_GET['special_id'])
		{
			if ($_GET['is_mobile'])
			{
				$url = '/mobile/special/' . $_GET['special_id'];
			}
			else
			{
				$url = '/special/' . $_GET['special_id'];
			}
		}
		else
		{
			$url = '/';
		}
		
		H::redirect_msg('发布成功, 请等待管理员审核...', $url);
	}
	public function save_ajax_action()
	{
		define('IN_AJAX', TRUE);
	
		$special_id = ($_GET['special_id']);
		$title = $_POST['title'];
		if (empty($title))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "专题标题不能为空"));
		}
	
		if($special_id)
		{
			$special = $this->model('special')->get_special_info_by_id($special_id,false);
		}
	
		if($_POST['url_token'])
		{
			if (!preg_match("/^(?!__)[a-zA-Z0-9_]+$/i", $_POST['url_token']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名只允许输入英文或数字'));
			}
				
			if (preg_match("/^[\d]+$/i", $_POST['url_token']) AND ($special_id != $_POST['url_token']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名不可以全为数字'));
			}
	
			if ($this->model('special')->check_url_token($_POST['url_token'], $special_id))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名已经被占用请更换一个'));
			}
		}
		//增加新专题
		$data = array(
				'title' => $title,
				'description' => htmlspecialchars($_POST['description']),
		//		'icon' => $_POST['icon'],
		//		'css' => htmlspecialchars($_POST['css']),
				'url_token' => $_POST['url_token'],
				'img_url' =>G_STATIC_URL.'/common/special_ico.png',
		);
		if (! $special_id)
		{
			$syncStr ="添加了专辑";
			$special_id = $this->model('special')->add_special($data);
			/*	if ($_POST['add_nav_menu'])
				{
			$this->model('menu')->add_nav_menu($title, htmlspecialchars($_POST['description']), 'special', $special_id);
			}
			*/
		}else{
			$syncStr ="编辑了专辑";
		}
	
		$update_data = array(
				'title' => $title,
				'description' => htmlspecialchars($_POST['description']),
		//		'css' => htmlspecialchars($_POST['css']),
				'url_token' => $_POST['url_token'],
		);
	//	print_r($_POST['description']);
	//	exit($special_id);
		$this->model('special')->update_special($special_id, $update_data);
		if($special_id){
			$url='special/explore/'.$special_id;
		}else{
			$url='special/main/';
		}
		$url =get_js_url("/".$url);
		//微博同步
		$do_syncQt=$_POST['do_syncQt'];
		$do_syncSt=$_POST['do_syncSt'];
		if($do_syncQt==1 || $do_syncSt==1){
			$content=$title;
			$img = $this->model('special')->get_special_info_by_id(intval($special_id));
			$picurl = $img['img_url'];
			$_POST=array();
			if(isset($picurl)){
				$start=strpos($picurl,"uploads");
				if(!$start){$start=strpos($picurl,"static");}
				$picurl=substr($picurl, $start);
				$_POST['pic']="@".$picurl;
				$_POST['pic_url']=$picurl;
			}else{
				$_POST['pic']="@static/common/special_ico.png";
				$_POST['pic_url']="static/common/special_ico.png";
			}
		
			$user_name=USER::get('__CLIENT_USER_NAME') ;
			$_POST['content']=$user_name."在【17find】http://www.17find.net".$syncStr."：【".$content."】".$url;
						$_POST['type']=1;
			//print_r($_POST);
		}
		//qq微博{
		if($do_syncQt==1){
			$qc = load_class('open_qc');
			$ret = $qc->add_pic_t($_POST);
			TPL::assign('ret', $ret);
		}
		//qq微博{
		//新浪微博{
		if($do_syncSt==1){
			$c = load_class('open_SaeTClientV2');
			$retSt = $c->upload($_POST['content'],$_POST['pic_url']);	//发送微博
			TPL::assign('retSt', $retSt);
		}
		//}新浪微博
		//微博同步				
		H::ajax_json_output(AWS_APP::RSM(array(
				'url' =>  $url
		), 1, "修改成功"));
	
	}
	public function ajax_search_action(){
		$keyword = $_GET['q'];
		$site_str = $this->model('special')->get_search_site($keyword);
		echo $site_str;
	}		
	public function qs_add_action(){
		$special_id=(int)($_GET['id']);
		TPL::assign('special_id', $special_id);
		TPL::output('special/edit_special_q');
	}	
	public function qs_edit_action(){
		$qs_info = $this->model('special')->get_qs_info(intval($_GET['id']),false);
		$special_id=$qs_info["special_id"];
		TPL::assign('special_id', $special_id);
		TPL::assign('qs_info', $qs_info);
		TPL::output('special/edit_special_q');
	}
	public function saveSite_ajax_action()
	{
		define('IN_AJAX', TRUE);
	
		$siteId = intval($_POST['siteId']);
		$comment = $_POST['comment'];
		$score = intval($_POST['score']);
		$special_id =intval($_POST['special_id']);
		$qs_id =intval($_POST['qs_id']);
		$isbook =intval($_POST['isbook']);
		$showtype = ($_POST['showtype']);
		if (empty($siteId))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请选择网站！"));
		}
		if($isbook && $special_id){
			//	echo $isbook.'-'.$special_id;
			$this->model('special')->update_book($special_id,$isbook);		//更新封面
		}
		if(!$qs_id)
		{
			if (!preg_match("/^(?!__)[a-zA-Z0-9_]+$/i", $score))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '评分错误'));
			}
	
			/* 			if (preg_match("/^[\d]+$/i", $_POST['url_token']) AND ($special_id != $_POST['url_token']))
				{
			H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名不可以全为数字'));
			} */
	
			if ($this->model('special')->check_site_token($siteId, $special_id))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '本专辑下已经存在该网站'));
			}
		}
		//增加网站
		$data = array(
				'question_id' => $siteId,
				'special_id' => $special_id,
				'comment' => $comment,
				'score' => $score,
		);
		if (!$qs_id)	//判断是添加还是更改
		{
			$qs_id = $this->model('special')->add_special_site($data);
		}
	
		$update_data = array(
				'question_id' => $siteId,
				'special_id' => $special_id,
				'comment' => $comment,
				'score' => $score,
		);
		// 		print_r($update_data);
		// 		echo $qs_id;
		//exit($showtype);
		$this->model('special')->update_qs($qs_id, $update_data);
		if($showtype==2){	//跳转类型
			$url='special/explore/'.$special_id;
		}else{
			$url='admin/special/site_list/special_id-'.$special_id;
		}
		$url =get_js_url("/".$url);
		//微博同步
		if($_POST['do_syncQt']==1){
			
			$sitename = $this->model('question')->get_question_info_by_id(intval($siteId));
			$sitename = $sitename['question_content'];			
			$info = $this->model('special')->get_special_info_by_id(intval($special_id));
			$content=$info['title'];
			$picurl = $info['img_url'];
			$_POST=array();
			if(isset($picurl)){
				$start=strpos($picurl,"uploads");
				if(!$start){
					$start=strpos($picurl,"static");
				}
				$picurl=substr($picurl, $start);
				$_POST['pic']="@".$picurl;
			}else{
				$_POST['pic']="@static/common/special_ico.png";
			}
			$user_name=USER::get('__CLIENT_USER_NAME') ;
			$_POST['content']=$user_name."在【17find】http://www.17find.net编辑了专辑：【".$content."】".$url."下的【".$sitename."】";
			$_POST['type']=1;
			//print_r($_POST);
			$qc = load_class('open_qc');
			$ret = $qc->add_pic_t($_POST);
			TPL::assign('ret', $ret);
		}
		//微博同步		
		H::ajax_json_output(AWS_APP::RSM(array(
				'url' =>  $url
		), 1, "修改成功"));
	
	}	
}